﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace xna_game_engine.source
{
    public class Warrior : BaseCharacter
    {
        protected int mNormalAttackMin, mNormalAttackMax;
        protected TongaTimer mNormalAtkTimer;
        protected List<SkillBase> mSkills;
        protected Dictionary<FaceButtons, SkillBase> mSkillMap;
        //protected SkillWarDevastate mDevastateSkill;
        //protected SkillWarBrutalize mBrutalizeSkill;

        public Warrior()
        {
            // Setup our members
            mStrength = 8;
            mAgility = 5;
            mFocus = 4;
            mSpeed = 5;// (int)(2.0f + (mAgility * 0.15));
            mName = "Triscuit";
            mCurHp = mMaxHp = 100;
            mCurPower = mMaxPower = 100;
            mFrameImage = GameGlobals.Content.Load<Texture2D>("sprites\\unit_frames\\player_image");
            mModel = GameGlobals.Content.Load<Texture2D>("sprites\\unit_frames\\player_image");
            mBoundingBox = new Rectangle((int)mPos.X, (int)mPos.Y, mModel.Width, mModel.Height);
            mGold = 25;
            mClass = "Arcanus Warrior";
            mPos = new Vector2(384.0f, 512.0f);
            mCurXP = 0.0f;
            mMaxXp = 100.0f;
            mNormalAttackMin = (int)(0.9f * (mStrength * 2));
            mNormalAttackMax = (int)(1.1f * (mStrength * 2));
            mNormalAtkTimer = new TongaTimer(0.6);
            SetupSkills();
            SetupTalents();
        }

        public override void Update(GameTime gt)
        {
            mHpRegenTick.Update(gt);
            mNormalAtkTimer.Update(gt);
            if (mHpRegenTick.Check())
            {
                mHpRegenTick.Reset();
                // Warrior regens 2% of max life per tick...for now
                ModHealth(mMaxHp * .02f, Color.Green);
            }

            // Lets scale the the thumbstick delta by the speed of the player
            UpdatePosition();
            foreach (SkillBase skill in mSkills)
            {
                skill.Update(gt);
            }
            //mDevastateSkill.Update(gt);
        }

        public override void LevelUp()
        {
            // TODO: Handle all leveling up info
            if (mCurLevel < 99)
            {
                mCurLevel++;
                // Tweak these with balance
                mStrength += 3;
                mAgility += 2;
                mFocus += 1;
                mMaxHp += mStrength / 2;
                mCurHp = mMaxHp;
                mNormalAttackMin = (int)(0.9f * (mStrength*2));
                mNormalAttackMax = (int)(1.1f * (mStrength*2));
            }
        }

        public override void NormalAttack()
        {
            Enemy target = GameGlobals.EnemyManager.GetEnemyClosestToPointWithinRange(mPos, 150.0f);
            if (target != null)
            {
                if (mNormalAtkTimer.Check())
                {
                    mNormalAtkTimer.Reset();
                    float damage = GameGlobals.RNG.Next(mNormalAttackMin, mNormalAttackMax) * 100;
                    bool critical = (GameGlobals.RNG.NextDouble() < 0.25);//mAgility / 1000.0f);
                    target.ModHealth((critical) ? -(2 * damage) : -damage, Color.DarkRed);
                    if (target.IsDead)
                    {
                        AwardKill(target);
                    }
                }
            }
        }

        public override void OnSkillUse(FaceButtons button)
        {
            if (mSkillMap.ContainsKey(button))
            {
                mSkillMap[button].Use();
            }
        }

        public override void AddTalentPoint(int index_in_tree)
        {
            mTalentTree.AddTalentPoint(index_in_tree);
        }

        // Private helpers
        private void UpdatePosition()
        {
            mPos.X += mSpeed * (GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X);
            mPos.Y += mSpeed * -(GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y);
            mBoundingBox.Location = new Point((int)mPos.X, (int)mPos.Y);
        }

        private void SetupSkills()
        {
            mSkills = new List<SkillBase>();
            mSkillMap = new Dictionary<FaceButtons, SkillBase>();
            mSkills.Add(new SkillWarDevastate(this));
            mSkills.Add(new SkillWarBrutalize(this));
            mSkillMap[FaceButtons.A_BUTTON] = mSkills[0];
            mSkillMap[FaceButtons.B_BUTTON] = mSkills[1];
            //mDevastateSkill = new SkillWarDevastate(this);
            //mBrutalizeSkill = new SkillWarBrutalize(this);
        }

        public void MapSkill(SkillBase skill, FaceButtons button)
        {
            mSkillMap[button] = skill;
        }

        private void SetupTalents()
        {
            mTalentTree = new CTalentTree("Tree");
            mTalentTree.AddTalent(new TalentWarStoutHearted(this));
        }
    }
}
